package com.avalon.erp.sys.addon.base;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.PrimaryKey;
import com.avalon.core.model.View;
import com.avalon.core.module.AbstractModule;
import com.avalon.core.service.AbstractService;
import com.avalon.core.util.Dom4jUtils;
import com.avalon.core.util.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BaseModule extends AbstractModule {
    @Override
    public String getModuleName() {
        return "base";
    }

    @Override
    public String getLabel() {
        return "基础模块";
    }

    @Override
    public String getDescription() {
        return "基础模块 系统登录,权限,菜单等功能";
    }

    @Override
    public Boolean getDisplay() {
        return false;
    }

    /**
     * 定制安装模块 顺序
     */
    @Override
    public void createModule() {
        getContext().getServiceBean("base.service.log").createTable(); // 优先日志表
        getContext().getServiceBean("base.module").createTable(); // 优先创建模块表
        PrimaryKey key = insertModuleInfo();
        getContext().getServiceBean("base.service").createTable(); // 创建服务表
        getContext().getServiceBean("base.field").createTable(); // 创建字段表

        if (ObjectUtils.isNull(getServiceList())) return;
        for (AbstractService service : getServiceList()) {
            if (service.getServiceName().equals("base.module")) {
                PrimaryKey serviceId = service.insertTableInfo(key);
                service.insertFieldInfo(serviceId);
                continue;
            }
            service.createTable();
            PrimaryKey serviceId = service.insertTableInfo(key);
            service.insertFieldInfo(serviceId);
        }

        try {
            View view = Dom4jUtils.loadModule(getModuleName());
            if (ObjectUtils.isNull(view)) {
                return;
            }

        } catch (DocumentException | AvalonException e) {
            e.printStackTrace();
        }
    }
}
